package com.jason.apps.dashboard;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.R;
import com.forimageandlist.Adapter;
import com.forimageandlist.ImageLoader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class EditAccount extends SherlockFragment{
   
	EditPersonOnClickSelectedListener mCallback;

	EditPersonAdapter adapter;
	ConsumeWebService c;
	public ImageLoader imageLoader; 
	
	final static ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
	
	HashMap<String, String> map;
	private ListView list;
	
	public static final String KEY_FIRSTNAME = "firstName";
	public static final String KEY_LASTNAME = "lastName";
	public static final String KEY_MOBILE = "mobile";
	public static final String KEY_LANDLINE = "landLine";
	public static final String KEY_PERSON_ID = "person_id";
	public static final String KEY_EMAILADDRESS = "emailAddress";
	public static final String KEY_USERNAME = "userName";
	public static final String KEY_PASSWORD = "password";
	public static final String KEY_IMAGE = "person_image";
	public static final String KEY_BIRTHDAY = "birthday";
	public static final String KEY_TYPE = "type";
	SherlockFragmentActivity fa;
	
	public void displayList(){
		
		
		 String data = c.getAllPerson();
		 
		 try {

				JSONArray array = new JSONArray(data);
				
				for (int i = 0; i < array.length(); i++) {
					map = new HashMap<String, String>();
					JSONObject e = array.getJSONObject(i);
					map.put("person_id", e.getString("person_id"));
					map.put("firstName", e.getString(KEY_FIRSTNAME)+" "+e.getString(KEY_LASTNAME));
					map.put("fname", e.getString(KEY_FIRSTNAME));
					map.put("lname", e.getString(KEY_LASTNAME));
					map.put("mobile", e.getString(KEY_MOBILE)+"/ "+e.getString(KEY_LANDLINE));
					map.put("mob", e.getString(KEY_MOBILE));
					map.put("land", e.getString(KEY_LANDLINE));
					map.put("person_id", e.getString(KEY_PERSON_ID));
					map.put("emailAddress", e.getString(KEY_EMAILADDRESS));
					map.put("username", e.getString(KEY_USERNAME));
					map.put("password", e.getString(KEY_PASSWORD));
					map.put("person_image", e.getString(KEY_IMAGE));
					map.put("type", e.getString(KEY_TYPE));
					map.put("birthday", e.getString(KEY_BIRTHDAY));

					mylist.add(map);
					System.out.println(e.getString("person_image"));
				}
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString()+"diri jud");
			}finally{
				
			}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		fa = (SherlockFragmentActivity) super.getActivity();
		View view=inflater.inflate(R.layout.listforeditproperty, container, false);
		c = new ConsumeWebService();
		mylist.clear();
		
		adapter=new EditPersonAdapter(getActivity(),mylist);
		list=(ListView)view.findViewById(R.id.list_editproperty);
	    new Load().execute();

		return view;
    }
	 @Override
	    public void onAttach(Activity activity) {
	        super.onAttach(activity);
	        
	        try {
	            mCallback = (EditPersonOnClickSelectedListener) activity;
	        } catch (ClassCastException e) {
	            throw new ClassCastException(activity.toString()
	                    + " must implement OnHeadlineSelectedListener");
	        }
	    }
	 
	 public interface EditPersonOnClickSelectedListener {
	        /** Called by HeadlinesFragment when a list item is selected */
	        public void EditAccountonArticleSelected(View v, int position);
	    }
	 
	 public class Load extends AsyncTask<Void, Void, Void> {

		    private final ProgressDialog dialog = new ProgressDialog(fa);

		    protected void onPreExecute() {

		            this.dialog.setMessage("Loading...");
		            this.dialog.show();
		    }
		    protected Void doInBackground(final Void... unused) {

		    	try {
					displayList();	
			
				} catch (Exception e) {
					Log.e("","wala jud ma add",e);
				}
		        return null; 
		    }
		    protected void onPostExecute(Void result) {
		    	
		    	list.setAdapter(adapter);
		    	
		    	if (this.dialog!=null) {
		            this.dialog.dismiss();
		            }  
		     }
		  }
	
    }
    

